package com.cn.sl.server.common.model

import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.FetchType
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.MappedSuperclass
import org.springframework.data.annotation.CreatedBy
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedBy
import org.springframework.data.annotation.LastModifiedDate
import java.time.LocalDateTime

/**
 * 所有DO实体的父类
 *
 * @author 阮雪峰
 */
@MappedSuperclass
open class EditableEntity<UserEntity : BaseEntity> : BaseEntity() {
    @CreatedBy
    @Column(name = "created_by")
    var createdById: Long? = null

    @ManyToOne(fetch = FetchType.LAZY, cascade = [CascadeType.DETACH])
    @JoinColumn(name = "created_by", insertable = false, updatable = false)
    var createdBy: UserEntity? = null

    @CreatedDate
    @Column(updatable = false)
    var createdTime: LocalDateTime = LocalDateTime.now()

    @LastModifiedDate
    @Column
    var updatedTime: LocalDateTime = LocalDateTime.now()

    @LastModifiedBy
    @Column(name = "updated_by")
    var updatedById: Long? = null

    @ManyToOne(fetch = FetchType.LAZY, cascade = [CascadeType.DETACH])
    @JoinColumn(name = "updated_by", insertable = false, updatable = false)
    var updatedBy: UserEntity? = null
}